﻿import flash.display.InteractiveObject;
import flash.display.Sprite;
import flash.text.TextField;
import flash.text.TextFieldAutoSize;
import flash.text.TextFormat;
import mclight.skins.IPushButtonSkin;
import mclight.utils.Maths;

class PushButtonSkin implements IPushButtonSkin
{
	var _sprite:Sprite;
	
	var _radius:Float;
	
	var _border:Float;
	
	var _rectNormal:VectorRect;
	
	var _rectOver:VectorRect;
	
	var _rectDown:VectorRect;
	
	var _rectDisabled:VectorRect;
	
	var _rectSelectedNormal:VectorRect;
	
	var _rectSelectedOver:VectorRect;
	
	var _rectSelectedDown:VectorRect;
	
	var _rectSelectedDisabled:VectorRect;
	
	var _formatNormal:TextFormat;
	
	var _formatOver:TextFormat;
	
	var _formatDown:TextFormat;
	
	var _formatDisabled:TextFormat;
	
	var _formatSelectedNormal:TextFormat;
	
	var _formatSelectedOver:TextFormat;
	
	var _formatSelectedDown:TextFormat;
	
	var _formatSelectedDisabled:TextFormat;
	
	var _paddingLeft:Int;
	
	var _paddingRight:Int;
	
	var _paddingTop:Int;
	
	var _paddingBottom:Int;
	
	var _tf:TextField;
	
	public function new() 
	{
		_radius = 4;
		_border = 4;
		_rectNormal = new VectorRect([0xefefef, 0xa0a0a0], [.9, .7], [.0, 255], [0x707070, 0x202020], [1., 1.], [.0, 255], 1);
		_rectOver = new VectorRect([0xffffff, 0xa0a0a0], [.9, .7], [.0, 255], [0x707070, 0x202020], [1., 1.], [.0, 255], 1);
		_rectDown = new VectorRect([0xffffff, 0xa0a0a0], [.9, .7], [.0, 255], [0x707070, 0x202020], [1., 1.], [.0, 255], 2);
		_rectDisabled = new VectorRect([0xffffff, 0xa0a0a0], [.5, .5], [.0, 255], [0x707070, 0x202020], [.7, .7], [.0, 255], 1);
		_rectSelectedNormal = _rectDown;
		_rectSelectedOver = _rectDown;
		_rectSelectedDown = _rectDown;
		_rectSelectedDisabled = new VectorRect([0xffffff, 0xa0a0a0], [.5, .5], [.0, 255], [0x707070, 0x202020], [.7, .7], [.0, 255], 2);
		_formatNormal = new TextFormat('Tahoma', 12, 0x303030);
		_formatOver = new TextFormat('Tahoma', 12, 0x303030);
		_formatDown = new TextFormat('Tahoma', 12, 0x303030);
		_formatDisabled = new TextFormat('Tahoma', 12, 0x808080);
		_formatSelectedNormal = _formatDown;
		_formatSelectedOver = _formatDown;
		_formatSelectedDown = _formatDown;
		_formatSelectedDisabled = _formatDown;
		_paddingLeft = _paddingRight = 5;
		_paddingTop = _paddingBottom = 2;
		_sprite = new Sprite();
		object = _sprite;
		_tf = new TextField();
		_tf.autoSize = TextFieldAutoSize.LEFT;
		_tf.mouseEnabled = false;
		_tf.selectable = false;
		_sprite.addChild(_tf);
		state = NORMAL;
	}
	
	static var _tfCalculating:TextField;
	
	public function calculateMinSize()
	{
		var tf = _tfCalculating;
		if (tf == null)
		{
			tf = _tfCalculating = new TextField();
			tf.autoSize = TextFieldAutoSize.LEFT;
		}
		tf.defaultTextFormat = _formatNormal;
		tf.text = text;
		minWidth = Std.int(_paddingLeft + tf.width + _paddingRight);
		minHeight = Std.int(_paddingTop + tf.height + _paddingBottom);
	}
	
	public function update()
	{
		var rectStyle:VectorRect;
		var textFormat:TextFormat;
		if (selected)
		{
			switch(state)
			{
				case NORMAL:
					rectStyle = _rectSelectedNormal;
					textFormat = _formatSelectedNormal;
				case OVER:
					rectStyle = _rectSelectedOver;
					textFormat = _formatSelectedOver;
				case DOWN:
					rectStyle = _rectSelectedDown;
					textFormat = _formatSelectedDown;
				case DIZABLED:
					rectStyle = _rectSelectedDisabled;
					textFormat = _formatSelectedDisabled;
			}
		}
		else
		{
			switch(state)
			{
				case NORMAL:
					rectStyle = _rectNormal;
					textFormat = _formatNormal;
				case OVER:
					rectStyle = _rectOver;
					textFormat = _formatOver;
				case DOWN:
					rectStyle = _rectDown;
					textFormat = _formatDown;
				case DIZABLED:
					rectStyle = _rectDisabled;
					textFormat = _formatDisabled;
			}
		}
		
		rectStyle.draw(_sprite.graphics, 0, 0, width, height, _radius);
		_tf.text = text;
		_tf.setTextFormat(textFormat);
		
		_tf.x = Maths.center(width, _tf.width);
		_tf.y = Maths.center(height, _tf.height);
	}
	
	public var state:State;
	
	public var selected:Bool;
	
	public var width:Int;
	
	public var height:Int;
	
	public var text:String;
	
	public var object(default, null):InteractiveObject;
	
	public var minWidth(default, null):Int;
	
	public var minHeight(default, null):Int;
}